class Solution:
    def maxSlidingWindow(self, nums: list, k: int) -> list:
        if not nums:
            return []
        if k == 1:
            return nums
        length = len(nums)
        res, index, max_num = [max(nums[:k])], nums[:k].index(max(nums[:k])), max(nums[:k])
        for i in range(k, length):
            if nums[i] >= max_num:
                index, max_num = k - 1, nums[i]
            else:
                index -= 1
                if index < 0:
                    mid_list = nums[i-k+1:i+1]
                    index, max_num = mid_list.index(max(mid_list)), max(mid_list)
            res.append(max_num)
        return res


a = Solution()
print(a.maxSlidingWindow([1, 3, -1, -3, 5, 3, 6, 7], 3))
print(a.maxSlidingWindow([1, -1], 1))

